package office.poi.excel.utils;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;

public class ColumnUtils {
	 /**
	   * 根据列内容得到列索引
	   * @param sheet
	   * @param columnName
	   * @return
	   */
	  public static int[] getColumnIndex(HSSFSheet sheet,String columnName)
	  {
		  List<HSSFCell> list =  CellUtils.findCell(sheet,columnName);
		  int[] index = new int[list.size()];
		  try{ 
			 for (int i = 0; i < list.size(); i++) {
				 index[i] = list.get(i).getColumnIndex();
			}
		  }catch (Exception e) {
		}
		  return index;	 
	  }
	  public static int[] getColumnIndexByRowIndex(final HSSFSheet sheet,final String columnName,final int rowIndex)
	  {
		  List<HSSFCell> list =  CellUtils.findCell(sheet,columnName,rowIndex);
		  int[] index = new int[list.size()];
		  try{ 
			 for (int i = 0; i < list.size(); i++) {
				 index[i] = list.get(i).getColumnIndex();
			}
		  }catch (Exception e) {
		}
		  return index;	
	  }
	  public static int getFirstColumnIndexByRowIndex(final HSSFSheet sheet,final String columnName,final int rowIndex)
	  {
		  HSSFCell cell =  CellUtils.getFirstCell(sheet,columnName, rowIndex);
		  if(cell != null)return cell.getColumnIndex();
		  return -1;
	  }
	  
	  /**
	   * 判断某列是否存在
	   * @param sheet
	   * @param columnName
	   * @return
	   */
	  public static boolean isExistColumn(HSSFSheet sheet,String columnName)
	  {
		  if(getColumnIndex(sheet, columnName).length>=0)return true;
		  return false;
	  }
}
